home *** CD-ROM | disk | FTP | other *** search
- /*
- PrintfExit.c
-
- PrintfExit() is normally equivalent to calling printf and exit.
-
- Require(quickDrawVersion) checks that the computer is providing the environment
- that your program needs, and fails gracefully if not.
-
- Lots of VideoToolbox routines, when they find a really grievous error, print out
- a message and exit. Having one routine that does both makes the code slightly
- neater, since the if statement then doesn't need braces. Furthermore, both of
- these functions, printf, and exit are liable to break in foreign environments,
- e.g when running as a MEX resource under MatLab. Now the problem is confined to
- this file, where it can be handled by conditional compilation.
-
- Since PrintfExit is called only when we're near death, it seems prudent to make sure
- there's enough stack space before calling printf, since it crashes if there's
- less than about 4500 byts of stack. Note that StackGrow() moves memory.
-
- I've replaced all calls to exit() in the entire VideoToolbox by calls to
- PrintfExit().
-
- HISTORY:
- 2/20/93 dgp Wrote it based on conversation with David Brainard.
- */
- #include "VideoToolbox.h" // StackGrow()
- #include <stdarg.h> // for variable-number-of-argument macros
- #include "mc68881.h"
-
- int PrintfExit(char *format,...)
- {
- va_list args;
- int i;
- long value=0;
-
- // The main program may have changed the current device. Let's
- // restore the main device before doing the printf.
- Gestalt(gestaltQuickdrawVersion,&value);
- if(value>=gestalt8BitQD)SetGDevice(GetMainDevice());
-
- if(StackSpace()<6000)StackGrow(6000-StackSpace());
- #ifndef MATLAB
- // printf crashes if there's less than about 4500 bytes of stack space
- if(StackSpace()>5000){
- va_start(args,format);
- i=vfprintf(stdout,format,args);
- va_end(args);
- }else SysBeep(20);
- exit(1);
- #else
- {
- char s[512];
-
- va_start(args,format);
- i=vsprintf(s,format,args);
- va_end(args);
- mex_error(s); // Ask MatLab to report the error.
- }
- #endif
- }
-
- void Require(long quickDrawVersion)
- // Call this at the beginning of your main program, so that your program
- // will fail gracefully, instead of crashing, when someone runs it
- // on a computer that lacks what your program needs.
- // The fpu and cpu tests automatically track your current compiler settings.
- {
- long value;
- OSErr error;
-
- error=Gestalt(gestaltFPUType,&value);
- if(error)PrintfExit("Sorry, I require Gestalt(). Your System is too old!\n");
- if(mc68881 && value==0)
- PrintfExit("Sorry. I've been compiled to use a floating point chip,"
- " and you don't have one.\n");
- error=Gestalt(gestaltProcessorType,&value);
- if(mc68020 && value<gestalt68020)
- PrintfExit("Sorry. I've been compiled to use a 68020 processor (or better),"
- " and you don't have one.\n");
- Gestalt(gestaltQuickdrawVersion,&value);
- if(value<quickDrawVersion)switch(quickDrawVersion){
- case gestalt8BitQD:
- PrintfExit("Sorry. This program requires Color QuickDraw.\n");
- default:
- PrintfExit("Sorry. This program requires 32-bit QuickDraw.\n");
- }
- }